home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-04-12 | 11.0 KB | 418 lines | [TEXT/MPS ] |
-
- (**********************************************************************************)
- (* T E x t e n d e d T e x t *)
- (**********************************************************************************)
-
- { By Calvin E. Cock (a.k.a. "Earnie") - Applications Design Group }
- { 60 E. Hanover Road }
- { Morris Plains, NJ 07950 }
-
- { R E V I S I O N H I S T O R Y }
-
- { 9/16/88 - Creation of TExtended Text Library Unit }
- { 9/28/88 - Corrected field identifier in FIELDS }
- { 10/10/88 - Changed Min & Max to Extended, cleaned up code }
-
- {$IFC qProceduralViews}
- {$S DlgOpen}
- {-----------------------------------+
- | IExtendedText |
- +-----------------------------------}
- PROCEDURE TExtendedText.IExtendedText(itsSuperView: TView;
- itsLocation, itsSize: VPoint;
- itsMinimum, itsMaximum : Extended;
- itsPrecision: integer;
- itsValue : str255 );
- Var
- IntHolder: LongInt;
- ExtHolder: Extended;
- Mask: DecForm;
-
-
- BEGIN
- IEditText(itsSuperView, itsLocation, itsSize, 255);
- if itsminimum > itsmaximum then
- begin
- {$IFC qDebug}
- Warning ( 1 );
- {$ENDC}
- ExtHolder := itsminimum;
- itsminimum := itsmaximum;
- itsmaximum := ExtHolder;
- end;
- if itsMaximum = itsMinimum then
- begin
- {$IFC qDebug}
- Warning( 2 );
- {$ENDC}
- itsMaximum := itsMaximum + 1;
- end;
- fMinimum := itsMinimum;
- fMaximum := itsMaximum;
- fPrecision := itsPrecision;
- if fPrecision > MaxPrec then
- begin
- {$IFC qDebug}
- PrecWarning;
- {$ENDC}
- fPrecision := MaxPrec;
- end;
- if itsValue <> '' then { allow null string entries }
- begin
- ExtHolder := Str2Num ( itsValue );
- if (ExtHolder < itsMinimum) or (ExtHolder > itsMaximum) then
- begin
- {$IFC qDebug}
- Warning ( 3 );
- {$ENDC}
- { Convert the value to the decimal precision required }
- Mask.Digits := MaxPrec;
- Mask.Style := FixedDecimal;
- Num2Str ( Mask , itsMinimum , DecStr(itsValue) );
- end;
- end;
- SetText( itsValue , kDontRedraw);
- fValue := itsValue;
- END;
- {$ENDC}
-
-
- {$IFC qTemplateViews}
- {$S DlgOpen}
- {-----------------------------------+
- | IRes |
- +-----------------------------------}
- PROCEDURE TExtendedText.IRes (itsDocument: TDocument; itsSuperView: TView;
- VAR itsParams: Ptr); OVERRIDE;
-
- Var
- IntHolder: integer;
-
- BEGIN
- INHERITED IRes(NIL, itsSuperView, itsParams);
-
- WITH ExtendedTextTemplatePtr(itsParams)^ DO
- BEGIN
- if minimum > maximum then
- begin
- {$IFC qDebug}
- Warning ( 1 );
- {$ENDC}
- IntHolder := minimum;
- minimum := maximum;
- maximum := IntHolder;
- end;
- if minimum = maximum then
- begin
- {$IFC qDebug}
- Warning( 2 );
- {$ENDC}
- Maximum := Maximum + 1;
- end;
- fMinimum := minimum;
- fMaximum := maximum;
- fPrecision := prec;
- if fPrecision > MaxPrec then
- begin
- {$IFC qDebug}
- PrecWarning;
- {$ENDC}
- fPrecision := MaxPrec;
- end;
-
- { Would like to check to see if Value is within Min/Max at this }
- { point but cannot because we would have to call Str2Num to convert }
- { the value into a extended type to check it. Str2Num is one of }
- { those nasty routines that can move memory around and it will cause}
- { a crash here. Believe me, I tried. }
-
- { Because of the above, we cannot force the decimal precision of the}
- { initial value loaded from the resource by calling Str2Num and }
- { Num2Str. Just make sure that your initial value string in your }
- { resource is formatted correctly according to your dec precision. }
-
- fValue := value;
- SetText(value, kDontRedraw);
- {$IFC qDebug}
- if gIntenseDebugging then
- begin
- Writeln ( 'fMinimum = ' , fMinimum );
- Writeln ( 'fMaximum = ' , fMaximum );
- Writeln ( 'fPrecision = ' , fPrecision );
- Writeln ( 'fValue = ' , fValue );
- end;
- {$ENDC}
- END;
-
- OffsetPtrWStr(itsParams, SIZEOF(ExtendedTextTemplate));
- END;
- {$ENDC}
-
-
- {$IFC qWriteTemplates}
- {$S MAWriteRes}
- {-----------------------------------+
- | WRes |
- +-----------------------------------}
- PROCEDURE TExtendedText.WRes (theResource: ViewRsrcHndl; VAR itsParams: Ptr); OVERRIDE;
-
- VAR
- nmPtr: ExtendedTextTemplatePtr;
- Mask: DecForm;
- theValue: Str255;
-
- BEGIN
- INHERITED WRes(theResource, itsParams);
-
- GetText ( theValue ); { Get the text so we can get the length }
-
- nmPtr := ExtendedTextTemplatePtr(ExpandPtrWStr(theResource, itsParams,
- SIZEOF(ExtendedTextTemplate) , Length(theValue) ));
-
- WITH nmPtr^ DO
- BEGIN
- Mask.Style := FixedDecimal;
- Mask.Digits := fPrecision;
- Num2Str ( Mask , GetValue , DecStr(Value) );
- minimum := Num2LongInt (fMinimum);
- maximum := Num2LongInt (fMaximum);
- prec := fPrecision;
- END;
- END;
-
- {$S MAWriteRes}
- {-----------------------------------+
- | WriteRes |
- +-----------------------------------}
- PROCEDURE TExtendedText.WriteRes (theResource: ViewRsrcHndl;
- VAR itsParams: Ptr); OVERRIDE;
- BEGIN
- gWResSignature := 'xnum'; { This MUST be unique. }
- gWResType := 'TExtendedText';
- WRes(theResource, itsParams);
- END;
- {$ENDC}
-
-
- {$S DlgRes}
- {-----------------------------------+
- | NotNull |
- +-----------------------------------}
-
- { NotNull will return TRUE if the current string in the TExtendedText edit }
- { box is not ''. Use this before the call to GetValue to make sure that }
- { an entry has been made. i.e. IF NotNull THEN myNum := GetValue }
-
- FUNCTION TExtendedText.NotNull: Boolean;
-
- var
- aString : Str255;
- begin
- GetText ( aString );
- NotNull := aString <> '';
- end;
-
- {-----------------------------------+
- | GetValue |
- +-----------------------------------}
- FUNCTION TExtendedText.GetValue: Extended;
-
- VAR
- aString: Str255;
- theValue: Extended;
-
- BEGIN
- GetText(aString);
- if aString <> '' then
- begin
- theValue := Str2Num(aString);
- GetValue := theValue;
- fValue := aString;
- end
- else
- GetValue := Self.fMinimum; { if its null, we must set it to something }
- END; { See NotNull function above. }
-
-
- {$S DlgNonRes}
- {-----------------------------------+
- | SetValue |
- +-----------------------------------}
- PROCEDURE TExtendedText.SetValue (newValue: Extended; redraw: BOOLEAN);
-
- VAR
- aString: DecStr;
- Mask: DecForm;
-
- BEGIN
- if newValue < fMinimum then newValue := fMinimum;
- if newValue > fMaximum then newValue := fMaximum;
- Mask.digits := fprecision;
- Mask.style := FixedDecimal;
- Num2Str ( Mask , newValue , aString );
- SetText(aString, redraw);
- fValue := aString;
- END;
-
-
- {$S DlgNonRes}
- {-----------------------------------+
- | Validate |
- +-----------------------------------}
- FUNCTION TExtendedText.Validate: LONGINT; OVERRIDE;
-
- VAR
- theString: Str255;
- decRec: Decimal;
- extValue: Extended;
- index: INTEGER;
- validPrefix: BOOLEAN;
-
- BEGIN
- Validate := kValidValue;
-
- GetText(theString);
- IF theString = '' THEN
- theString := '0';
-
- index := 1;
- Str2Dec(theString, index, decRec, validPrefix);
- IF validPrefix & (index > LENGTH(theString)) THEN
- BEGIN
- extValue := Dec2Num(decRec);
- IF extValue < fMinimum THEN
- Validate := kValueTooSmall
- ELSE IF extValue > fMaximum THEN
- Validate := kValueTooLarge;
- END
- ELSE
- Validate := kNonNumericCharacters;
- END;
-
-
- {$S DlgRes}
- {-----------------------------------+
- | DoKeyCommand |
- +-----------------------------------}
-
- { This will filter out any keys other than Numbers, the Enter key, the Return key, }
- { the Tab key, the Backspace key and, if the Textended OBJECT will accept negative }
- { values, the minus key. }
-
- Function TExtendedText.DoKeyCommand ( ch : char; aKeyCode: integer;
- VAR info : eventInfo):TCommand;OVERRIDE;
-
- var
-
- GoodSet : set of char;
-
- begin
- if (fTEView <> NIL) then
- begin
- GoodSet := ['0','1','2','3','4','5','6','7','8','9','.',
- chEnter,chReturn,chTab,chBackSpace];
- if fMinimum < 0 then GoodSet := GoodSet + ['-'];
- if NOT (ch in GoodSet) then
- begin
- SysBeep ( 5 ); { Maybe take this annoying beep out??? }
- DoKeyCommand := gNoChanges;
- end
- else
- DoKeyCommand := INHERITED DoKeyCommand ( ch , aKeyCode , info );
- end;
- end;
-
- {$IFC qDebug}
- {$S DlgFields}
- {-----------------------------------+
- | Fields |
- +-----------------------------------}
-
- { Be sure to include the MyFieldToStr procedure in your main unit and }
- { set the gFieldToStrRtn variable to point to it in you IApplication }
- { P.S. Dont forget the constants either. }
-
- PROCEDURE TExtendedText.Fields (PROCEDURE DoToField (fieldName: Str255;
- fieldAddr: Ptr;
- fieldType: INTEGER)); OVERRIDE;
-
- BEGIN
- DoToField('TExtendedText', NIL, bClass);
- DoToField('fValue', @fValue, bString);
- DoToField('fMinimum', @fMinimum, bExtended);
- DoToField('fMaximum', @fMaximum , bExtended);
- DoToField('fPrecision' , @fPrecision , bInteger);
- INHERITED Fields(DoToField);
- END;
-
- {-----------------------------------+
- | Warning |
- +-----------------------------------}
-
- PROCEDURE TExtendedText.Warning( ErrorNum : integer);
-
- var
- message0 : str255;
- message1 : str255;
- message2 : str255;
- loop : integer;
-
- begin
- CASE ErrorNum of
- 1 : begin
- Message0 := 'Your Minimum value is larger than your ';
- Message1 := 'Maximum in the TExtendedText View ';
- Message2 := 'They have been switched.';
- end;
- 2 : begin
- Message0 := 'Your Minimum and Maximum values are equal in';
- Message1 := 'the TExtendedText View ';
- Message2 := 'Maximum has been set to Maximum + 1';
- end;
- 3 : begin
- Message0 := 'Your value is not within the range of your';
- Message1 := 'Min and Max values in the TExtendedText View ';
- Message2 := 'It has been set to your Min value.';
- end;
- end; {CASE}
-
- Writeln ('***************************************************');
- Writeln ('* ! ! ! W A R N I N G ! ! ! *');
- Writeln ('***************************************************');
- Writeln;
- Writeln ( message0 );
- Write ( message1 );
- for loop := 1 to 4 do write ( fIdentifier[loop]);
- Writeln ( '.' );
- Writeln ( message2 );
- Writeln;
- SysBeep ( 5 );
- end;
-
- {-----------------------------------+
- | PrecWarning |
- +-----------------------------------}
-
- PROCEDURE TExtendedText.PrecWarning;
-
- var
- loop : integer;
-
- begin
- Writeln ('***************************************************');
- Writeln ('* ! ! ! W A R N I N G ! ! ! *');
- Writeln ('***************************************************');
- Writeln;
- Writeln ( 'A precision value more than' , MaxPrec:1 , ' is unusual. Your');
- Writeln ( 'value is presently set at: ' , fPrecision:2 , ' for the' );
- Write ( 'TExtendedTextView ');
- for loop := 1 to 4 do Write ( fIdentifier[loop] );
- Writeln ( '. It has been reset to ' , MaxPrec:1 , '.');
- Writeln;
- SysBeep ( 5 );
- end;
-
-
- {$ENDC}
-
- end.